package com.qdu.web.controller.bussiness;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.qdu.bussiness.mapper.BusRentMapper;
import com.qdu.common.core.domain.model.LoginUser;
import com.qdu.common.utils.SecurityUtils;
import com.qdu.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qdu.common.annotation.Log;
import com.qdu.common.core.controller.BaseController;
import com.qdu.common.core.domain.AjaxResult;
import com.qdu.common.enums.BusinessType;
import com.qdu.bussiness.domain.BusBill;
import com.qdu.bussiness.service.IBusBillService;
import com.qdu.common.utils.poi.ExcelUtil;
import com.qdu.common.core.page.TableDataInfo;

/**
 * 账单信息Controller
 *
 * @author qdu
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/bussiness/bill")
public class BusBillController extends BaseController
{
    @Autowired
    private IBusBillService busBillService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private BusRentMapper busRentMapper;

    /**
     * 查询账单信息列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBill busBill)
    {
        startPage();
        List<BusBill> list = busBillService.selectBusBillList(busBill);
        return getDataTable(list);
    }

    /**
     * 导出账单信息列表
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:export')")
    @Log(title = "账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBill busBill)
    {
        List<BusBill> list = busBillService.selectBusBillList(busBill);
        ExcelUtil<BusBill> util = new ExcelUtil<BusBill>(BusBill.class);
        util.exportExcel(response, list, "账单信息数据");
    }

    /**
     * 获取账单信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return AjaxResult.success(busBillService.selectBusBillByBillId(billId));
    }

    /**
     * 新增账单信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:add')")
    @Log(title = "账单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusBill busBill)
    {
        return toAjax(busBillService.insertBusBill(busBill));
    }

    /**
     * 修改账单信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:edit')")
    @Log(title = "账单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBill busBill)
    {
        return toAjax(busBillService.updateBusBill(busBill));
    }

    /**
     * 删除账单信息
     */
    @PreAuthorize("@ss.hasPermi('bussiness:bill:remove')")
    @Log(title = "账单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds)
    {
        return toAjax(busBillService.deleteBusBillByBillIds(billIds));
    }

    /**
     * 查询账单详情
     * @param billId
     * @return
     */
    @GetMapping("/details/{billId}")
    public AjaxResult getBillDetails(@PathVariable Long billId){
        return busBillService.getBillDetails(billId);
    }

    /**
     * 结算帐单
     * @param billId
     * @return
     */
    @GetMapping("/settle/{billId}")
    public AjaxResult settleBill(@PathVariable Long billId){
        AjaxResult ajaxResult = busBillService.settleBill(billId);
        if(ajaxResult.get(AjaxResult.CODE_TAG).equals(200)){
            // 查询账号余额
            String balance = busRentMapper.selectUserBalanceByUserName(SecurityUtils.getUsername());
            LoginUser loginUser = getLoginUser();
            loginUser.getUser().setBalance(balance);
            tokenService.setLoginUser(loginUser);
        }
        return ajaxResult;
    }
}
